import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/common/event_bus.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/login&register/login_textfield.dart';
import 'package:find_friend/pages/login&register/model/shared_user_info.dart';
import 'package:flutter/material.dart';

class MineModSignPage extends StatefulWidget {
  @override
  _MineModSignPageState createState() => _MineModSignPageState();
}

class _MineModSignPageState extends State<MineModSignPage> {
  TextEditingController _controller = TextEditingController();
  int maxlength = 16;
  FocusNode focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    _initData();
  }

  _initData() async {
    SharedUserInfo user = await SharedUserInfo.instance();
    _controller.text = user.bio;
    maxlength = 16 - user.bio.length <= 0 ? 0 : 16 - user.bio.length;
    if(mounted) setState(() {});
  }

  _request() {
    Networktool.request(API.edit, params: {"bio": _controller.text}, success: (data){
      print(data);
      if(data["data"] != null) {
        SharedUserInfo.saveData(data);
        AlertTool.showToast("修改成功");
        EventMineBus.instance.commit(EventMineKeys.UpdateMineInfo, null);
        Navigator.of(context).pop(true);
      }
    },
    fail: (msg) => AlertTool.showToast(msg));
  }

  @override
  Widget build(BuildContext context) {

    final size = MediaQuery.of(context).size;

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.white,
        leading: IconButton(
          onPressed: (){
            Navigator.of(context).pop();
          },
          icon: Image.asset("assets/fanhui.png")
        ),
        title: Text("修改我的个签"),
        actions: [
          FlatButton(
            onPressed: _request,
            child: Text("完成", style: instanceTextStyle(14, APP_font_black,fontWeight: FontWeight.normal)),
          )
        ],
      ),
      body: SafeArea(
        child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: (){
            focusNode.unfocus();
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 37.5),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Container(
                  margin: const EdgeInsets.symmetric(vertical: 10,),
                  height: 40,
                  child: TextField(
                      controller: _controller,
                      maxLength: 16,
                      maxLines: 5,
                      focusNode: focusNode,
                      onEditingComplete: () => focusNode.unfocus(),
                      decoration:InputDecoration(
                            hintText: "请输入个性签名",
                            counterText: "",
                            labelStyle: instanceTextStyle(14, APP_font_black),
                            hintStyle: instanceTextStyle(14, APP_font_grey),
                            focusedBorder: OutlineInputBorder(
                            borderRadius: const BorderRadius.all(Radius.circular(25)),
                            borderSide: const BorderSide(width: 1.5, color: Colors.black),
                          ),
                          enabledBorder: OutlineInputBorder(
                            borderRadius: const BorderRadius.all(Radius.circular(25)),
                            borderSide: const BorderSide(width: 1.5, color: Colors.black),
                          ),
                          border: OutlineInputBorder(
                            borderRadius: const BorderRadius.all(Radius.circular(25)),
                            borderSide: const BorderSide(width: 1.5, color: Colors.black),
                          ),
                          contentPadding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
                      ),
                      onChanged: (str) {
                        setState(() {
                          maxlength = 16 - str.length <= 0 ? 0 : 16 - str.length;
                        });
                      },
                      autofocus: false,
                    ),
                  ),
                Container(
                  padding: const EdgeInsets.only(right: 10),
                  child: Text("$maxlength",style: instanceTextStyle(11, APP_font_grey),)
                ),
                Spacer(),
                GestureDetector(
                  onTap: _request,
                  child: Container(
                    width: size.width,
                    height: 40,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.black, width: 1.5),
                      borderRadius: BorderRadius.circular(20)
                    ),
                    margin: const EdgeInsets.symmetric(horizontal: 62, vertical: 50),
                    child: Text("保存", style: instanceTextStyle(18, APP_font_black),),
                  ),
                )
              ],
            )
          ),
        ),
      )
    );
  }
}